﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ImapX.Parsing.Tokens
{
    public class NList<T>:Token where T:Token, new() 
    {
        private T _token = new T();
        private List<T> _list = new List<T>();

        public List<T> List { get { return _list; } }

        public NList(bool canBeNIL = true)
        {
            _token.CanBeNIL = false;
        }

        protected override string BeginPattern
        {
            get
            {
                return @"\("; 
            }
        }

        protected override string EndPattern
        {
            get
            {
                return @"\)";
            }
        }

        protected override string ParseParts(string input)
        {
            while (!IsMatch(input, EndPattern))
            {
                input = input.TrimStart(' ');

                T t = new T();
                input = t.Parse(input);
                _list.Add(t);
                
            }
            return input;
        }

        public T this[int index] {
            get
            {
                return _list[index];
            }
        }

        public void Add(T t)
        {
            _list.Add(t);
        }

        public void Remove(T t)
        {
            _list.Remove(t);
        }

        public void Clear()
        {
            _list.Clear();
        }

        public IEnumerator<T> GetEnumerator()
        {
            return _list.GetEnumerator();
        }
   }
}
